"""
Case Type   : 资源池化故障模式库
Case Name   : 审计目录不存在-审计目录不存在
Create At   : 2025/04
Owner       : haomeng
Description :
    1、修改操作审计开关为全开
    2、查看集群状态
    3、运行tpcc业务
    4、删除主节点审计目录
    5、查看业务运行情况
    6、重启集群
    7、查看集群状态，查看审计目录是否存在
    8、恢复环境
Expect      :
    1、参数设置成功
    2、集群状态正常
    3、运行业务成功
    4、删除主节点pg_log目录成功
    5、业务运行正常无影响
    6、重启集群成功
    7、集群状态正常，产生新的审计目录
    8、恢复环境成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

class SSFauleMode(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = [Node(node='PrimaryRoot'), Node(node='PrimaryDbUser')]
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()

        self.audit_param = 'audit_system_object'

    def test_ss_faule(self):
        text = '-----step1:修改操作审计开关为全开  expect:参数设置成功-----'
        self.log.info(text)

        self.log.info('-----获取审计操作默认值-----')
        self.param_value = self.pri_sh.execut_db_sql(f'show {self.audit_param};')
        self.default_value = self.param_value.splitlines()[2].strip()
        self.log.info(self.default_value)

        result = self.pri_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                           f'{self.audit_param} = 268435455')
        self.log.info(result)
        self.assertTrue(result, '参数设置失败')

        text = '-----step2:查看集群状态;  expect:集群状态正常-----'
        self.log.info(text)

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('Normal', status, '集群状态异常')

        text = '-----step3:执行tpcc读写业务  expect:业务执行成功-----'
        self.log.info(text)

        tpcc_cmd = f'./runBenchmark.sh {macro.PROPS_FILENAME}'
        self.log.info(tpcc_cmd)

        tpcc_execute = ComThread(self.com.start_tpcc, args=(self.user_node[0],
                                                               f'{macro.TPCC_PATH}',
                                                               tpcc_cmd))
        tpcc_execute.setDaemon(True)
        tpcc_execute.start()

        text = '-----step4:删除主节点pg_audit审计目录  expect:删除成功-----'
        self.log.info(text)

        delete = f'rm -rf {macro.PG_AUDIT_PATH};ls {macro.PG_AUDIT_PATH}'
        self.log.info(delete)

        delete_execute = ComThread(self.com.get_sh_result, args=(self.user_node[1], delete))
        delete_execute.setDaemon(True)
        delete_execute.start()

        text = '-----step5:查看业务运行情况  expect:业务运行正常无影响-----'
        self.log.info(text)

        tpcc_execute.join(10 * 60)
        ret1 = tpcc_execute.get_result()
        self.log.info(ret1)
        self.assertIn('Session End', ret1, 'tpcc执行失败')

        delete_execute.join(10 * 60)
        ret2 = delete_execute.get_result()
        self.log.info(ret2)
        self.assertIn('No such file or directory', ret2, '删除pg_log目录失败')

        text = '-----step6:重启集群  expect:重启成功-----'
        self.log.info(text)

        restart = self.pri_sh.exec_cm_ctl('stop;', 'cm_ctl start')
        self.log.info(restart)
        self.assertIn(self.constant.cm_stop_success_msg, restart, '集群重启失败')
        self.assertIn(self.constant.cm_start_success_msg, restart, '集群重启失败')

        text = '-----step7:查看集群状态，查看pg_audit目录是否存在  expect:集群状态正常，产生新的pg_audit目录-----'
        self.log.info(text)

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '集群重启失败')

        check = f'ls {macro.PG_AUDIT_PATH}'
        self.log.info(check)
        res = self.user_node[1].sh(check).result()
        self.log.info(res)
        self.assertIn('dn_6001', res, 'pg_audit目录不存在')

    def tearDown(self):
        text = '-----step8:恢复环境  expect:恢复环境成功-----'
        self.log.info(text)

        recovery = self.pri_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                             f'{self.audit_param} = {self.default_value}')
        self.log.info(recovery)
        self.assertTrue(recovery, '参数恢复失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
